package com.example.yls.demoa;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;

/**
 * Created by yls on 2018/12/17.
 */

public class Myservice extends Service {
  private Boolean isLive = true;
  private MyBinder myBinder;
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        Log.e("MyService","onBind");
        return myBinder;
    }
    public void onCreate(){
        super.onCreate();
        Log.e("MyService","onCreate");
        myBinder = new MyBinder();
       new Thread(new Runnable() {
           @Override
           public void run() {
               while (isLive){
                   try{
                       Thread.sleep(2000);

                   }catch (InterruptedException e){
                       e.printStackTrace();
                   }
                   Log.e("MyService","time = " + System.currentTimeMillis());
               }
           }
       }).start();
    }
   public int onStartCommand(Intent intent,int flags,int startId){
        Log.e("MyService","onStartCommand");
        return super.onStartCommand(intent,flags,startId);
   }
   public void onDestroy() {
       isLive = false;
       super.onDestroy();
       Log.e("MyService", "onDestroy");
   }
}
class MyBinder extends Binder{
    public int add(int a,int b){return a+b;}
}
